package com.h2cloud.JUCTest.CountDownLatch;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * @Package: com.h2cloud.JUCTest.CountDownLatch
 * @ClassName: A2
 * @Author: h2cloud
 * @CreateTime: 2022/9/25 11:26 上午
 * @Description:
 */
@Slf4j
public class A2 {
	volatile List list = new ArrayList<>();

	public void add(int i) {
		list.add(i);
	}

	public int getSize() {
		return list.size();
	}

	public static void main(String[] args) {
		A2 a2 = new A2();
		CountDownLatch countDownLatch = new CountDownLatch(1);
		new Thread(() -> {
			if (a2.getSize() != 5) {
				try {
					countDownLatch.await();
					log.info("countDownLatch await end");
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}

		}, "t1").start();

		new Thread(() -> {
			for (int i = 0; i < 10; i++) {
				a2.add(i);
				log.info("add:{}", i);
				if (a2.getSize() == 5){
					log.info("countDownLatch is open");
					countDownLatch.countDown();
				}

			}
		}, "t2").start();

	}

}
